package com.hnqcgczy.supplier.controller;

import com.hnqcgczy.supplier.service.ShareholderService;
import com.hnqcgczy.supplier.service.impl.ShareholderServiceImpl;
import com.hnqcgczy.supplier.utils.BaseServlet;
import com.hnqcgczy.supplier.vo.Shareholder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "ShareholderServlet", value = "/ShareholderServlet")
public class ShareholderServlet extends BaseServlet {
    //需要服务层
    private ShareholderService shareholderService = new ShareholderServiceImpl();

    public void addShareholder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        Shareholder shareholder = new Shareholder();
        shareholder.setShareholderName(request.getParameter("shareholder_name"));
        shareholder.setCompanyId(Integer.parseInt(request.getParameter("company_id")));
        shareholder.setShareholdingRatio(request.getParameter("shareholding_ratio"));
        boolean flag = shareholderService.add(shareholder);
        response.sendRedirect("ShareholderServlet");

    }

    public void queryAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        String shareholder_name = request.getParameter("shareholder_name") != null ? request.getParameter("shareholder_name") : "";
        Shareholder query = new Shareholder();
        query.setShareholderName(shareholder_name);
        int pageNow = request.getParameter("pageNow") != null ? Integer.parseInt(request.getParameter("pageNow")) : 1;
        int pageSize = 10;

        int totals = shareholderService.totals(query);
        if (pageNow <= 1) {
            pageNow = 1;
        }
        if (pageNow >= totals) {
            pageNow = totals;
        }
        List<Shareholder> shareholders = shareholderService.findByLike(query, pageNow, pageSize);
        int pages = totals % pageSize == 0 ? totals / pageSize : totals / pageSize + 1;
        //存
        request.setAttribute("pageNow", pageNow);
        request.setAttribute("shareholders", shareholders);
        request.setAttribute("totals", totals);
        request.setAttribute("pages", pages);
        //跳
        request.getRequestDispatcher("Shareholder.jsp").forward(request, response);
    }

    public void updateById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        Shareholder shareholder = new Shareholder();
        shareholder.setShareholderId(request.getParameter("shareholder_id") != null ? Integer.parseInt(request.getParameter("shareholder_id")) : 0);
        shareholder.setShareholderName(request.getParameter("shareholder_name"));
        shareholder.setCompanyId(Integer.parseInt(request.getParameter("company_id")));
        shareholder.setShareholdingRatio(request.getParameter("shareholding_ratio"));
        boolean flag = shareholderService.update(shareholder);
        response.sendRedirect("ShareholderServlet");
    }

    public void deleteById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        int shareholder_id = request.getParameter("shareholder_id") != null ? Integer.parseInt(request.getParameter("shareholder_id")) : 0;
        boolean flag = shareholderService.delete(shareholder_id);
        response.sendRedirect("ShareholderServlet");
    }
}
